package com.example.mywanandroid.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.mywanandroid.util.CallUtil;


public class LocationService extends Service {
    private static final String TAG = "LocationService";

    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private IBinder binder = new LocationService.LocalBinder();
    private final String ChannelId = "NotificationChannel_LocationService";

    private NotificationManager manager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        super.onCreate();
        //前台初始化
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            showNotify();
            Log.d(TAG, "26: ");
        }

    }

    // 定义内容类继承Binder
    public class LocalBinder extends Binder {
        //返回本地服务
        public LocationService getService() {
            return LocationService.this;
        }
    }

    private void showNotification(String title, String context) {
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(
                    ChannelId,
                    title,
                    NotificationManager.IMPORTANCE_LOW);
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setDescription(context);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = (new NotificationCompat.Builder(this, ChannelId))
                .setContentTitle(title)
                .setContentText(context)
                .setAutoCancel(false)
                .setOngoing(true)
                .setTicker(title);
        Notification notification = builder.build();
        startForeground(1, notification);
    }

    //显示通知栏
    @SuppressLint("NewApi")
    public void showNotify() {
        //调用这个方法把服务设置成前台服务
        startForeground(CallUtil.NOTIFY_ID, CallUtil.buildNotification(getApplicationContext()));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //初始化定位sdk
        initLocation();
        return super.onStartCommand(intent, flags, startId);
    }

    private void initLocation() {
        Log.d(TAG, "initLocation: ");
        try {
            mLocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(false);
            if (mLocationOption.isOnceLocationLatest()) {
                mLocationOption.setOnceLocationLatest(true);
                //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。
                //如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会。
            }
            //设置是否强制刷新WIFI，默认为强制刷新
            mLocationOption.setWifiActiveScan(true);
            //设置是否允许模拟位置,默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(1000);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.getMessage());
        }
    }

    public void getLocation(AMapLocationListener aMapLocationListener) {
        initLocation();
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.startLocation();
        mLocationClient.setLocationListener(aMapLocationListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
            mLocationClient = null;
            mLocationOption = null;
        }
    }
}
